<template>
  <!-- 修改用户名
       1:定义接口api
       2:导入接口api
       3:绑定确定点击事件
       4：确定点击事件中调用接口api
       5:调用成功：
          提示一下
          关闭弹框
          更新用户信息 -->
  <!-- 修改性别  -->
  <!-- 1：绑定确定点击事件 -->
  <!-- 2：确定点击事件中调用接口api -->
  <!-- 3：调用成功： -->
  <!-- 提示一下 -->
  <!-- 关闭弹框 -->
  <!-- 更新用户信息   -->
  <div class="edit">
    <van-nav-bar
      left-arrow
      title="编辑资料"
      @click-left="$router.push('/layout/mine')"
    ></van-nav-bar>
    <van-uploader :after-read="afterRead">
      <div class="user-avatar">
        <img class="avatar" :src="userInfo.photo" alt="" />
      </div>
    </van-uploader>
    <van-cell-group>
      <van-cell
        title="名称"
        @click="nameClick"
        :value="userInfo.name"
        is-link
      />
      <van-cell
        title="性别"
        :value="userInfo.gender === 0 ? '女' : '男'"
        is-link
        @click="genderClick"
      />
      <van-cell
        title="生日"
        @click="birthdayClick"
        :value="userInfo.birthday"
        is-link
      />
    </van-cell-group>

    <!-- 修改名称弹框 -->
    <!-- <button @click="$refs.xxx.focus()">获取输入框焦点</button>
    <van-field ref="xxx"></van-field> -->
    <van-dialog
      v-model="showName"
      title="修改名称"
      show-cancel-button
      @confirm="confirmName"
    >
      <van-field v-model.trim="nameField" ref="nameField"></van-field>
    </van-dialog>
    <!-- 修改性别 弹框 -->
    <van-popup v-model="showGender" position="bottom">
      <van-nav-bar title="修改性别" left-text="取消"></van-nav-bar>
      <van-cell
        v-if="userInfo.gender === 0"
        title="男"
        is-link
        @click="setGenter(1)"
      ></van-cell>
      <van-cell
        v-if="userInfo.gender === 1"
        title="女"
        is-link
        @click="setGenter(0)"
      ></van-cell>
    </van-popup>
    <!-- 生日弹框 -->
    <van-popup v-model="showBirthday" position="bottom">
      <van-datetime-picker
        @cancel="showBirthday = false"
        @confirm="confirmBirthday"
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
      />
    </van-popup>
    <!-- 头像修改弹框 -->
    <div class="setAvatar" v-if="showAvatar">
      <!-- ref:1:原生标签：获取dom, 组件标签：获取组件this -->
      <vueCropper
        ref="cropper"
        :img="cropperImg"
        autoCrop
        autoCropWidth="200"
        autoCropHeight="200"
      >
      </vueCropper>
      <div class="btn">
        <van-button type="primary" @click="submit">裁剪</van-button>
        <van-button type="primary" @click="showAvatar = false">取消</van-button>
      </div>
    </div>
  </div>
</template>
<script>
// import { userProfile } from '@/api/mine'
import { mapState } from 'vuex'
import { userProfilePatch, userPhoto } from '@/api/mine'
import moment from 'moment'
import { VueCropper } from 'vue-cropper'
export default {
  components: {
    VueCropper
  },
  data () {
    return {
      showName: false,
      nameField: '',
      showGender: false,
      showBirthday: false,
      currentDate: new Date(),
      minDate: new Date('1971-01-01'),
      maxDate: new Date(),
      showAvatar: false,
      cropperImg: ''
    }
  },
  computed: {
    ...mapState(['userInfo'])
  },
  async created () {
    // const res = await userProfile()
    // 存储到vuex
    // this.$store.commit('setUserInfo', res.data)
    // console.log('用户信息', res)
    // this.$store.dispatch('getUserInfo')
    this.$store.dispatch('getUserInfo')
  },
  mounted () {
    // this.$store.dispatch('getUserInfo')
  },
  methods: {
    // 名称点击
    nameClick () {
      this.showName = true
      // 显示名称
      this.nameField = this.userInfo.name
      // 聚焦
      setTimeout(() => {
        this.$refs.nameField.focus()
      }, 600)
    },
    // 修改姓名确定事件
    async confirmName () {
      // 有修改且名字不为空
      if (this.nameField !== this.userInfo.name && this.nameField !== '') {
        await userProfilePatch({
          name: this.nameField
        })
        // 提示一下
        this.$toast.success('修改成功')
        // 更新用户信息
        this.$store.commit('setUserInfo', {
          ...this.userInfo,
          name: this.nameField
        })
      }
    },
    // 性别 点击事件
    genderClick () {
      this.showGender = true
    },
    // 性别 修改
    async setGenter (num) {
      await userProfilePatch({
        gender: num
      })
      // 关闭弹框
      this.showGender = false
      // 提示一下
      this.$toast.success('修改成功')
      // 更新用户信息
      this.$store.commit('setUserInfo', {
        ...this.userInfo,
        gender: num
      })
    },
    // 生日点击
    birthdayClick () {
      this.showBirthday = true
      this.currentDate = new Date(this.userInfo.birthday)
    },
    // 修改生日确定点击
    async confirmBirthday () {
      // console.log(moment(this.currentDate).format('YYYY-MM-DD'))
      await userProfilePatch({
        birthday: moment(this.currentDate).format('YYYY-MM-DD')
      })
      // 提示
      this.$toast.success('修改成功')
      // 关闭弹框
      this.showBirthday = false
      // 更新数据
      this.$store.commit('setUserInfo', {
        ...this.userInfo,
        birthday: moment(this.currentDate).format('YYYY-MM-DD')
      })
    },
    // 上传头像
    afterRead (res) {
      // console.log(res.content)
      // console.log(res.file)
      // 打开弹框
      this.showAvatar = true
      this.cropperImg = res.content
    },
    // 切剪确定点击
    submit () {
      this.$refs.cropper.getCropBlob(async data => {
        const f = new FormData()
        f.append('photo', data)
        const res = await userPhoto(f)
        console.log(res.data.photo)
        // 提示
        this.$toast.success('上传头像成功')
        // 关闭弹框
        this.showAvatar = false
        // 更新用户头像数据
        // 更新数据
        this.$store.commit('setUserInfo', {
          ...this.userInfo,
          photo: res.data.photo
        })
      })
    }
  }
}
</script>
<style lang="less" scoped>
.user-avatar {
  width: 100vw;
  padding: 15px 0;
  text-align: center;
  .avatar {
    width: 90px;
    height: 90px;
    border-radius: 50%;
    object-fit: cover;
  }
}
.van-field {
  border: 1px solid #ccc;
}
.setAvatar {
  position: fixed;
  left: 0;
  top: 46px;
  bottom: 0;
  background-color: #fff;
  width: 100vw;
  .btn {
    position: absolute;
    left: 0;
    bottom: 0;
    display: flex;
    width: 100vw;
    justify-content: space-between;
  }
}
</style>
